package com.vertygoeclypse.multitimer;

import android.app.Dialog;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.app.DialogFragment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;


public class UpdateDialog extends DialogFragment {
    Button   mgupbtn, mgextbtn;
    EditText mgtagvalue, mgsecondsvalue;
    static Spinner  mgs;
    String value1, value2, value3;
    onSubmitUpdateListener uptgListener;
    onSubmitUpdateListener uptimListener;
    onSubmitUpdateListener upposListener;

    interface onSubmitUpdateListener{
        void updateOStagListener(String arg);
        void updateOSTimeListener(String arg);
        void updateOSPosListener(String arg);
    }
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){
    final Dialog mgcusd = new Dialog(getActivity(),R.style.Theme_MultiTimer_Dialog);
    mgcusd.setContentView(R.layout.managetimers);
    mgcusd.show();
    mgupbtn = (Button) mgcusd.findViewById(R.id.updatebtn);
    mgextbtn = (Button) mgcusd.findViewById(R.id.exitmtbtn);
    mgtagvalue = (EditText) mgcusd.findViewById(R.id.mgedittad);
    mgsecondsvalue = (EditText) mgcusd.findViewById(R.id.mgsecondsedit);
    mgs = (Spinner)mgcusd.findViewById(R.id.managedspinner);
    MainActivity upspinActivity = (MainActivity)getActivity();
    upspinActivity.mgLoadSpinnerData();
    mgupbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MainActivity myActivity = (MainActivity) getActivity();
            myActivity.updateOStagListener(mgtagvalue.getText().toString());
            myActivity.updateOSTimeListener(mgsecondsvalue.getText().toString());
            myActivity.updateOSPosListener(value3);
            myActivity.updateTimerresults();
            myActivity.mgLoadSpinnerData();
            myActivity.LoadSpinnerData();
        }
    });
    mgextbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mgcusd.dismiss();
        }
    });
    mgs.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView <?> parent, View v, int position, long l) {
            TimerDataBaseHandler mgdb = new TimerDataBaseHandler(MainActivity.PACKAGE_NAME);
            SQLiteDatabase mgdb1 = mgdb.getReadableDatabase();
            int tagposition1 = mgs.getSelectedItemPosition()+1;
            mgdb.getReadableDatabase();
            Cursor ccc = mgdb1.rawQuery("SELECT * FROM timers WHERE _id = "+tagposition1, null);
            if(ccc.moveToFirst()){
                mgtagvalue.setText(ccc.getString(1).toString());
                value1 = ccc.getString(1).toString();
                mgsecondsvalue.setText(ccc.getString(2).toString());
                value2 = ccc.getString(2).toString();
                value3 = String.valueOf(tagposition1).toString();
            }
        }
        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {}
    });
        return mgcusd;
    }
}
